﻿using BlazorApp3.Client.Application.Behaviors;
using FluentValidation;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace BlazorApp3.Client.Application;

public static class DependencyInjectionExtension
{
    public static IServiceCollection AddApplicationServices(
        this IServiceCollection services,
        IConfiguration config)
    {
        var assembly = typeof(DependencyInjectionExtension).Assembly;

        services.AddValidatorsFromAssembly(assembly);

        services.AddMediatR(config => {
            config.RegisterServicesFromAssembly(assembly);

            //config.AddBehavior(typeof(IPipelineBehavior<,>), typeof(AppLoggerBehavior<,>));
            //config.AddBehavior(typeof(IPipelineBehavior<,>), typeof(UnhandledExceptionLoggerBehavior<,>));
            //config.AddBehavior(typeof(IPipelineBehavior<,>), typeof(AdvancedAuthorizationBehaviour<,>));
            //config.AddBehavior(typeof(IPipelineBehavior<,>), typeof(AuthorizationBehaviour<,>));
            config.AddBehavior(typeof(IPipelineBehavior<,>), typeof(RequestValidationBehavior<,>));
            config.AddBehavior(typeof(IPipelineBehavior<,>), typeof(RequestPerformanceBehavior<,>));

            //config.NotificationPublisher = new TaskWhenAllPublisher();  //ForeachAwaitPublisher, 处理程序并行
        });
        return services;
    }
}